﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Wage
{

    public class WageServices
    {

        private WageRepository _wageRepository;
        public WageServices()
        {
            _wageRepository = new WageRepository();
        }
        public async Task<bool> FlipAmoutAsync(int id)
        {
            return await _wageRepository.FlipAmoutAsync(id);


        }
        public async Task<bool> UpdateItemAsync(SalaryItem salaryItem)
        {
            return await _wageRepository.UpdateAsync(salaryItem);

        }
        public async Task<List<SalaryItem>> FormatAsync(string value)

        {
            return await Task<List<SalaryItem>>.Run(() =>
            {
                List<SalaryItem> salaryItems = new List<SalaryItem>();
                ArrayList Names = new ArrayList();
                ArrayList Amounts = new ArrayList();

                var res = value.Split(new char[] { '\n', ' ', '\r', '\t', '\r' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var item in res)
                {
                    decimal amount = 0;
                    if (decimal.TryParse(item, out amount))
                    {

                        Amounts.Add(amount);
                    }
                    else
                    {
                        Names.Add(item);

                    }

                }
                if (Names.Count > Amounts.Count)
                {
                    for (int i = 0; i < Amounts.Count; i++)
                    {
                        salaryItems.Add(new SalaryItem { Amount = Convert.ToDecimal(Amounts[i]), Name = Names[i].ToString() });
                    }

                }
                else
                {
                    for (int i = 0; i < Names.Count; i++)
                    {
                        salaryItems.Add(new SalaryItem { Amount = Convert.ToDecimal(Amounts[i]), Name = Names[i].ToString() });
                    }


                }

                return salaryItems;
            });


        }



    }
}
